﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Windows;
using System.Collections;
using Microsoft.Windows.Controls.Ribbon;

namespace TouchUmlUI
{
    public class LocalizationManager
    {

        private static string currentLanguage = string.Empty;

        public static string CurrentLanguage
        {
            get
            {
                return currentLanguage;
            }
        }

        public static String CANNOT_SELECT;
        public static String POPUP_TITLE;
        public static String ASK_SAVE;
        public static String SAVE_EXCEPTION;
        public static String SUCC_EXPORT_IMAGE;
        public static String CONFIRM_DELETE_DIAGRAM;
        public static String SUCC_EXPORT_CSHARP;
        public static String FAIL_EXPORT_CSHARP;
        /*
        public static String SUCC_EXPORT_JAVA;
        public static String FAIL_EXPORT_JAVA;
        public static String SUCC_EXPORT_PHP;
        public static String FAIL_EXPORT_PHP;
        */

        public static String DEFAULT_FILE_NAME;
        public static String DEFAULT_DIAGRAM_NAME;
        public static String TOUCH_UML_DIALOG_FILTER;
        public static String ALL_FILES_DIALOG_FILTER;
        public static String BMP_DIALOG_FILTER;
        public static String JPG_DIALOG_FILTER;
        public static String TIFF_DIALOG_FILTER;
        public static String PNG_DIALOG_FILTER;
        public static String CSHARP_DIALOG_FILTER;

        public static String MODEL_CLASS_TITLE;
        public static String MODEL_INTERFACE_TITLE;
        public static String MODEL_METHODS_TITLE;
        public static String MODEL_ATTRIBUTES_TITLE;
        public static String MODEL_ASSOCIATIONS_TITLE;
        public static String MODEL_ACTION_TITLE;
        public static String MODEL_ACTOR_TITLE;
        public static String MODEL_NOTICE_TITLE;
      
        public class LanguageInfo 
        {
            String name;
            String langName;
            String fileName;

            public LanguageInfo(String name,String langName) 
            {
                this.name = name;
                this.langName = langName;
                this.fileName = langName + ".xml";
            }

            public String getFileName()
            {
                return this.fileName;
            }

            public String getLangName()
            {
                return this.langName;
            }

            public String getName()
            {
                return this.name;
            }

        }

        List<LanguageInfo> languageInfo = new List<LanguageInfo>();

        public List<LanguageInfo> Languages
        {
            get
            {
                return languageInfo;               
            }
        }

        public bool Load(String language,Window1 windowParent)
        {

            if (language == currentLanguage)
            {
                return true;
            }

            XmlDocument xmlDocument = new XmlDocument();
            LanguageInfo info = languageInfo.Find(delegate(LanguageInfo obj)
                {
                    return obj.getLangName().CompareTo(language) == 0;

                }
                );
            if (info != null)
            {
                xmlDocument.Load("languages\\" + info.getFileName());
                XmlNode main = xmlDocument.DocumentElement;
                // load other stuff (dialog filters. etc) DOKONCZYC!
                XmlNode other = main["Other"];
                DEFAULT_FILE_NAME = other["DefaultFileName"].InnerText;
                DEFAULT_DIAGRAM_NAME = other["DefaultDiagramName"].InnerText;
                TOUCH_UML_DIALOG_FILTER = other["TouchUMLDialogFilter"].InnerText;
                ALL_FILES_DIALOG_FILTER = other["AllFilesDialogFilter"].InnerText;
                BMP_DIALOG_FILTER = other["BmpDialogFilter"].InnerText;
                JPG_DIALOG_FILTER = other["JPGDialogFilter"].InnerText;
                TIFF_DIALOG_FILTER = other["TIFFDialogFilter"].InnerText;
                PNG_DIALOG_FILTER = other["PNGDialogFilter"].InnerText;
                CSHARP_DIALOG_FILTER = other["CSharpDialogFilter"].InnerText;

                MODEL_CLASS_TITLE = other["ModelClassTitle"].InnerText;
                MODEL_INTERFACE_TITLE = other["ModelInterfaceTitle"].InnerText;
                MODEL_METHODS_TITLE = other["ModelMethodsTitle"].InnerText;
                MODEL_ATTRIBUTES_TITLE = other["ModelAttributesTitle"].InnerText;
                MODEL_ASSOCIATIONS_TITLE = other["ModelAssociationsTitle"].InnerText;
                MODEL_ACTION_TITLE = other["ModelActionTitle"].InnerText;
                MODEL_ACTOR_TITLE = other["ModelActorTitle"].InnerText;
                MODEL_NOTICE_TITLE = other["ModelNoticeTitle"].InnerText;

                // load popups
                XmlNode popups = main["Popups"];
                CANNOT_SELECT = popups["CannotSelect"].InnerText;
                POPUP_TITLE = popups["PopupTitle"].InnerText;
                ASK_SAVE = popups["AskSave"].InnerText;
                SAVE_EXCEPTION = popups["SaveException"].InnerText;
                SUCC_EXPORT_IMAGE = popups["SuccExportImage"].InnerText;
                CONFIRM_DELETE_DIAGRAM = popups["ConfirmDeleteDiagram"].InnerText;
                SUCC_EXPORT_CSHARP = popups["SuccExportCSharp"].InnerText;
                FAIL_EXPORT_CSHARP = popups["FailExportCSharp"].InnerText;
                /*
                SUCC_EXPORT_JAVA = popups["SuccExportJava"].InnerText;
                FAIL_EXPORT_JAVA = popups["FailExportCSharp"].InnerText;
                SUCC_EXPORT_PHP = popups["SuccExportPhp"].InnerText;
                FAIL_EXPORT_PHP = popups["FailExportPhp"].InnerText;*/

                // load ribbon
                XmlNode ribbon = main["Ribbon"];
                windowParent.myribbon.Title = ribbon["Title"].InnerText;
                windowParent.MostRecentlyUsed.Text = " "+ribbon["MRU"].InnerText;
                XmlNode groups = ribbon["Groups"];                
                foreach (DictionaryEntry entry in windowParent.Resources)
                {
                    if (entry.Value is RibbonCommand)
                    {
                        RibbonCommand cmd = entry.Value as RibbonCommand;
                        String val = (entry.Key as String).Trim();
                        cmd.LabelTitle = groups[val].InnerText;                        
                    }
                }                
                XmlNode mainmenu = ribbon["MainMenu"];
                windowParent.myribbon.ApplicationMenu.ToolTip = ToolTipPanel.createStackPanel(mainmenu["ToolTipTitle"].InnerText,mainmenu["ToolTipDescription"].InnerText);                
                foreach (object o in windowParent.myribbon.ApplicationMenu.Items)
                {
                    if (o is RibbonApplicationMenuItem)
                    {
                        RibbonApplicationMenuItem menuItem = o as RibbonApplicationMenuItem;
                        XmlNode item = mainmenu[menuItem.Name];
                        (menuItem.Command as RibbonCommand).LabelTitle = item["LabelTitle"].InnerText;
                        (menuItem.Command as RibbonCommand).LabelDescription = item["LabelDescription"].InnerText;
                        menuItem.ToolTip = ToolTipPanel.createStackPanel(item["ToolTipTitle"].InnerText, item["ToolTipDescription"].InnerText);
                    }
                }

                foreach (RibbonTab tab in windowParent.myribbon.Tabs)
                {
                    XmlNode xmlTab = ribbon[tab.Name];
                    String tabLabel = xmlTab.Attributes[0].InnerText;
                    tab.Label = tabLabel;
                    foreach (RibbonGroup group in tab.Groups)
                    {
                        for (int i = 0;i<group.Controls.Count;i++)
                        {
                            object o = group.Controls[i];
                            if (o is RibbonButton)
                            {                                
                                RibbonButton rb = o as RibbonButton;
                                RibbonCommand cmd = rb.Command as RibbonCommand;                                
                                XmlNode item = xmlTab[rb.Name];
                                cmd.LabelTitle = item["LabelTitle"].InnerText;                            
                                rb.ToolTip = ToolTipPanel.createStackPanel(item["ToolTipTitle"].InnerText, item["ToolTipDescription"].InnerText);
                                rb.Command = null;
                                rb.Command = cmd;
                            }
                            else if (o is RibbonDropDownButton)
                            {
                                RibbonDropDownButton rb = o as RibbonDropDownButton;
                                RibbonCommand cmd = rb.Command as RibbonCommand;
                                XmlNode item = xmlTab[rb.Name];
                                cmd.LabelTitle = item["LabelTitle"].InnerText;                            
                                rb.ToolTip = ToolTipPanel.createStackPanel(item["ToolTipTitle"].InnerText, item["ToolTipDescription"].InnerText);
                                rb.Command = null;
                                rb.Command = cmd;
                            }
                            else if (o is RibbonToggleButton)
                            {
                                RibbonToggleButton rb = o as RibbonToggleButton;
                                RibbonCommand cmd = rb.Command as RibbonCommand;
                                XmlNode item = xmlTab[rb.Name];
                                cmd.LabelTitle = item["LabelTitle"].InnerText;
                                rb.ToolTip = ToolTipPanel.createStackPanel(item["ToolTipTitle"].InnerText, item["ToolTipDescription"].InnerText);
                                rb.Command = null;
                                rb.Command = cmd;
                            }
                        }
                    }
                }
                windowParent.myribbon.UpdateLayout();
                windowParent.myribbon.InvalidateVisual();
                return true;
            }
            return false;
        }

        public LocalizationManager()
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("languages\\main.xml");
            XmlNode node = xmlDocument.DocumentElement;
            XmlNodeList nodeList = node.ChildNodes;
            foreach (XmlNode n in nodeList)
            {
                XmlAttributeCollection attr = n.Attributes;
                languageInfo.Add(new LanguageInfo(attr[0].Value, attr[1].Value));
            }
            xmlDocument = null;
        }
    }
}
